﻿#include "FITKAbstractJunction.h"
#include "FITKAcousticsDiscModel.h"
#include "FITKAcousticsSEAModel.h"
#include "FITKAbstractSEASubSys.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"

#include <QDebug>

namespace Acoustics
{
    FITKAbstractJunction::~FITKAbstractJunction()
    {
        //虚函数
    }
    FITKAbstractJunction::JunctionType FITKAbstractJunction::getJunctiontType()
    {
        //错误类型
        return Acoustics::FITKAbstractJunction::JunctionType::NoneJunction;
    }

    void FITKAbstractJunction::initConnect(QList<int>& subSysIdList)
    {
        //设置连接
        _subSysIDList = subSysIdList;
        //更新连接数据
        this->updateConnect();
    }

    void FITKAbstractJunction::appendConnect(int subSysId)
    {
        //添加连接
        if (_subSysIDList.contains(subSysId)) return;
        _subSysIDList.append(subSysId);
        //更新连接数据
        this->updateConnect();
    }

    bool FITKAbstractJunction::getEnabledJunction()
    {
        //获取是否启用连接
        return _enabledJunction;
    }

    void FITKAbstractJunction::setEnabledJunction(bool enabled)
    {
        //设置是否启用连接
        _enabledJunction = enabled;
    }

    QList<int> FITKAbstractJunction::getSubSysIds()
    {
        //获取连接的子系统ID
        return _subSysIDList;
    }

    void FITKAbstractJunction::updateConnect()
    {
        //纯虚函数-更新连接数据
    }

    QList<FITKAbstractJunction*> FITKJunctionManager::getJointsByType(FITKAbstractJunction::JunctionType t)
    {
        QList<FITKAbstractJunction*> js;
        //遍历对象
        const int n = this->getDataCount();
        for (int i=0;i<n; ++i)
        {
            FITKAbstractJunction* j = this->getDataByIndex(i);
            if(nullptr == j) continue;
            //类型判断
            if (j->getJunctiontType() == t)
                js.append(j);
        }
        return js;
    }

    FITKAbstractJunction* FITKJunctionManager::getJunction(FITKAbstractJunction::JunctionType t, QList<int>& subSysIds)
    {
        //获取指定类型的耦合对象列表
        QList<FITKAbstractJunction*> junctionList = getJointsByType(t);

        for (FITKAbstractJunction* junction : junctionList) {
            //获取子系统ID
            QList<int> ids = junction->getSubSysIds();
            //判断连接子系统的一致性
            if (ids.size() != subSysIds.size()) continue;
            //判断子系统是否被包含
            bool isAllContains = true;
            for (int subSysID : subSysIds) {
                if (ids.contains(subSysID)) continue;
                isAllContains = false;
                break;
            }
            //如果所有的子系统都被包含，返回当前连接
            if (isAllContains)
                return junction;
        }
        return nullptr;
    }
}
